#!/usr/bin/env python
import sys
sys.path.append("../python")
import edbcli

if __name__ == "__main__":

	edb = edbcli.Client(["127.0.0.1:11911"])
	
	# test various malformed net shell handling:
	
#	def query(self, preamble_query, items = None ):
	preamble_query = "malformed query"
	items = [ {"blah":"foo"} ]
	"""
	preamble_query\r\n
	<number of items>\r\n
	<first item's attribute key list>\r\n
	<first item's attribute value>\r\n
	"""
	
	if items == None:		
		cmdstr = "%s\n0\n" % ( preamble_query, )
	else:
		cmdstr = "%s\n%d\n" % ( preamble_query, len(items) )
		
		for item in items:
			item_parts = []
			for k,v in item.iteritems():
				item_parts.append( "%s %d" % ( str(k), len(str(v))) )
			cmdstr += ",".join(item_parts)
			cmdstr += "\n"
			for k,v in item.iteritems():
				cmdstr += "%s\n" % ( str(v), )
			
	edb.sendcmd(cmdstr)
		
	results = edb.response()
	if len(results) == 0:
		print("OK")
	else:
		print("FAILURE")

	# test malformed 2:
	
	cmdstr = "INVALID QUERY REQUEST\n\r0\n\r"
	edb.sendcmd(cmdstr)
	results = edb.response()
	if len(results) == 0:
		print("OK")
	else:
		print("FAILURE")
		

	cmdstr = "INVALID QUERY REQUEST\n\r1\n\rblah 3\n\rblah's value should've been 3 bytes but this is much longer\n\r"
	edb.sendcmd(cmdstr)	# invalid request will be aborted
	results = edb.response()
	if len(results) == 1 and results[0]["result"] == "failure":
		print("OK")
	else:
		print("FAILURE")
		
	# at this point we fell off the track and would have to disconnect and reconnect
	# end of tests
	